package test.br.com.lh.urabuilder.helper
{
	import br.com.lh.urabuilder.helper.ArrowMover;
	import br.com.lh.urabuilder.helper.MouseEventHelper;
	import br.com.lh.urabuilder.model.UraModelLocator;
	import br.com.lh.urabuilder.view.arrow.ArrowContainer;
	
	import flash.geom.Point;
	
	import flexunit.framework.TestCase;
	import flexunit.framework.TestSuite;
	
	import mx.containers.Canvas;

	public class ArrowMoverTest extends TestCase
	{
		private var modelLocator:UraModelLocator;
		private var drawingCanvas:Canvas;
	
		public function ArrowMoverTest(methodName:String=null)
		{
			super(methodName);
		}
		
		public static function suite():TestSuite{
			var ts:TestSuite = new TestSuite();
			ts.addTest(new ArrowMoverTest("rotationShouldOnlyBeAllowedIfArrowIsSelected"));
			ts.addTest(new ArrowMoverTest("startPointShouldMoveAlongWithMouse"));
			ts.addTest(new ArrowMoverTest("endPointShouldMoveAlongWithMouse"));
			ts.addTest(new ArrowMoverTest("moveComponentOnScreen"));
			return ts;
		}
		
		public override function setUp():void{
			UraModelLocator.modelLocator = null;
			drawingCanvas= new Canvas();
			modelLocator = UraModelLocator.getInstance(drawingCanvas);
		}
		
		public function rotationShouldOnlyBeAllowedIfArrowIsSelected():void{
			var mouseEventHelper:MouseEventHelper = modelLocator.mouseEventHelper;
			var arrowMover:ArrowMover = new ArrowMover(mouseEventHelper,null);
			mouseEventHelper.isMouseRotatingStartArrow = false;
			assertFalse(arrowMover.rotateStartPoint(null));
		}
		
		public function startPointShouldMoveAlongWithMouse():void{
			var mouseEventHelper:MouseEventHelper = modelLocator.mouseEventHelper;
			mouseEventHelper.isMouseRotatingStartArrow = true;
			var arrowMover:ArrowMover = new ArrowMover(mouseEventHelper,null);
			var startPoint:Point = new Point(0,0);
			var endPoint:Point = new Point(10,10);
			var arrowContainer:ArrowContainer = modelLocator.arrowManager.insertArrow(startPoint,endPoint);
			var newPoint:Point = new Point(30,30);
			arrowMover.movingArrow = arrowContainer;
			arrowMover.rotateStartPoint(newPoint);
			assertEquals(1,drawingCanvas.getChildren().length);
			assertEquals(30,arrowContainer.startPoint.x);
			assertEquals(30,arrowContainer.startPoint.y);
			newPoint = new Point(31,31);
			arrowMover.movingArrow = arrowContainer;
			arrowMover.rotateStartPoint(newPoint);
			assertEquals(1,drawingCanvas.getChildren().length);
			assertEquals(31,arrowContainer.startPoint.x);
			assertEquals(31,arrowContainer.startPoint.y);
		}
		
		public function endPointShouldMoveAlongWithMouse():void{
			var mouseEventHelper:MouseEventHelper = modelLocator.mouseEventHelper;
			mouseEventHelper.isMouseRotatingEndArrow = true;
			var arrowMover:ArrowMover = new ArrowMover(mouseEventHelper,null);
			var startPoint:Point = new Point(0,0);
			var endPoint:Point = new Point(10,10);
			var arrowContainer:ArrowContainer = modelLocator.arrowManager.insertArrow(startPoint,endPoint);
			var newPoint:Point = new Point(30,30);
			arrowMover.movingArrow = arrowContainer;
			arrowMover.rotateEndPoint(newPoint);
			assertEquals(1,drawingCanvas.getChildren().length);
			assertEquals(30,arrowContainer.endPoint.x);
			assertEquals(30,arrowContainer.endPoint.y);
			newPoint = new Point(31,31);
			arrowMover.movingArrow = arrowContainer;
			arrowMover.rotateEndPoint(newPoint);
			assertEquals(1,drawingCanvas.getChildren().length);
			assertEquals(31,arrowContainer.endPoint.x);
			assertEquals(31,arrowContainer.endPoint.y);
		}
		
		public function moveComponentOnScreen():void{
			var mouseEventHelper:MouseEventHelper = modelLocator.mouseEventHelper;
			mouseEventHelper.isMouseMovingArrow = true;
			var arrowMover:ArrowMover = new ArrowMover(mouseEventHelper,null);
			var startPoint:Point = new Point(0,0);
			var endPoint:Point = new Point(10,10);
			var arrowContainer:ArrowContainer = modelLocator.arrowManager.insertArrow(startPoint,endPoint);
			var newPoint:Point = new Point(30,30);
			arrowMover.movingArrow = arrowContainer;
			assertEquals(1,drawingCanvas.getChildren().length);
			assertEquals(25,arrowContainer.startPoint.x);
			assertEquals(25,arrowContainer.startPoint.y);
			assertEquals(35,arrowContainer.endPoint.x);
			assertEquals(35,arrowContainer.endPoint.y);
		}
		
	}
}